package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.entity.Category;
import com.itheima.entity.Dish;
import com.itheima.entity.Setmeal;
import com.itheima.exception.CustExcepiton;
import com.itheima.mapper.CategoryMapper;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Slf4j
@Service
public class CategoryImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishsservice;

    @Autowired
    private SetmealService setmealService;

    @Override
    public Boolean deleteByID(Long id) {
        QueryWrapper<Dish> DishqueryWrapper = new QueryWrapper();
        DishqueryWrapper.select("count(*) as two");
        DishqueryWrapper.eq("category_Id", id);
        Map<String, Object> map = dishsservice.getMap(DishqueryWrapper);
        Long two = (Long) map.get("two");

        QueryWrapper<Setmeal> SetmealWrapper = new QueryWrapper();
        SetmealWrapper.select("count(*) as count");
        SetmealWrapper.eq("category_Id", id);
        Map<String, Object> maps = setmealService.getMap(SetmealWrapper);
        Long count = (Long) maps.get("count");

        if (two > 0) {
            throw new CustExcepiton("此菜系有关联菜品，不可删除");
        }
        if (count > 0) {
            throw new CustExcepiton("此套餐有关联菜品，不可删除");
        }
        super.removeById(id);
        return true;
    }
}
